//
//  YiViewController.m
//  YiLotteryDemo
//
//  Created by apple on 15/2/11.
//  Copyright (c) 2015年 coderyi. All rights reserved.
//

#import "YiViewController.h"
#import "YiLotteryCell.h"
#import "YiLotteryButton.h"

#import "YzDownView.h"

#define HScreen [[UIScreen mainScreen] bounds].size.height
#define WScreen [[UIScreen mainScreen] bounds].size.width
#define iOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0
@interface YiViewController ()
{
    
    UIView *lotteryView;
    UIView *lotteryBgView;
    YiLotteryCell *lotteryCell1;
    YiLotteryCell *lotteryCell2;
    YiLotteryCell *lotteryCell3;
    YiLotteryCell *lotteryCell4;
    YiLotteryCell *lotteryCell5;
    YiLotteryCell *lotteryCell6;
    YiLotteryCell *lotteryCell7;
    YiLotteryCell *lotteryCell8;
    YiLotteryCell *lotteryCell9;
    YiLotteryCell *lotteryCell10;
    YiLotteryCell *lotteryCell11;
    YiLotteryCell *lotteryCell12;
    YiLotteryCell *lotteryCell13;
    YiLotteryCell *lotteryCell14;
    YiLotteryCell *lotteryCell15;
    YiLotteryCell *lotteryCell16;
    YiLotteryCell *lotteryCell17;
    YiLotteryCell *lotteryCell18;
    YiLotteryCell *lotteryCell19;
    YiLotteryCell *lotteryCell20;
    YiLotteryCell *lotteryCell21;
    YiLotteryCell *lotteryCell22;
    YiLotteryCell *lotteryCell23;
    YiLotteryCell *lotteryCell24;
    
    YiLotteryButton *startButton;
    
    NSArray *array;
    NSMutableArray *_downBtnArr;
    NSTimer *timer;
    YiLotteryCell *currentView;
    
    YiLotteryCell *currentView1;
    YiLotteryCell *currentView2;
    YiLotteryCell *currentView3;
    UILabel *_currentReadyLab;
    
    NSInteger _totalFlowCount;
    NSInteger _getFlowCount;
    
    NSInteger _aKey;
    NSInteger _bKey;
    NSInteger _cKey;
    
    
    UILabel * _tempCountSaveLab;
    UILabel *_getExertLiuValueLab;
    
    YiLotteryCell *normalCurrentView;
    BOOL _isLucky;
}

@end

@implementation YiViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBarHidden=YES;
    _downBtnArr=[[NSMutableArray alloc]initWithCapacity:8];
    [self initLottery];
}



//初始化抽奖视图
-(void)initLottery
{
    lotteryBgView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, WScreen, HScreen)];
    [self.view addSubview:lotteryBgView];
    lotteryBgView.backgroundColor=[UIColor colorWithWhite:0.1 alpha:0.5];
    
    lotteryView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, WScreen, HScreen-0)];
    [lotteryBgView addSubview:lotteryView];
    lotteryView.backgroundColor=[UIColor whiteColor];
    
    float space=2;
    float cellWidth=30;
    float cellHeight=30;
    float orginY=75;
    float originX=(WScreen-(6*space+7*cellWidth))/2.0;
    
    lotteryCell1=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX, orginY, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell1];
    lotteryCell1.label.text=@"1";
    
    lotteryCell2=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth+space, orginY, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell2];
    lotteryCell2.label.text=@"2";
    lotteryCell3=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*2+space*2, orginY, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell3];
    lotteryCell3.label.text=@"3";
    lotteryCell4=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*3+space*3, orginY, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell4];
    lotteryCell4.label.text=@"4";
    
    ///
    lotteryCell5=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*4+space*4, orginY, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell5];
    lotteryCell5.label.text=@"5";
    lotteryCell6=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*5+space*5, orginY, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell6];
    lotteryCell6.label.text=@"6";
    lotteryCell7=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*6+space*6, orginY, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell7];
    lotteryCell7.label.text=@"7";
    
    lotteryCell8=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*6+space*6, orginY+cellHeight+space, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell8];
    lotteryCell8.label.text=@"8";
    
    lotteryCell9=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*6+space*6, orginY+(cellHeight+space)*2, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell9];
    lotteryCell9.label.text=@"9";
    
    lotteryCell10=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*6+space*6, orginY+(cellHeight+space)*3, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell10];
    lotteryCell10.label.text=@"10";
    
    lotteryCell11=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*6+space*6, orginY+(cellHeight+space)*4, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell11];
    lotteryCell11.label.text=@"11";
    
    lotteryCell12=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*6+space*6, orginY+(cellHeight+space)*5, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell12];
    lotteryCell12.label.text=@"12";
    
    lotteryCell13=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*6+space*6, orginY+(cellHeight+space)*6, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell13];
    lotteryCell13.label.text=@"13";
    
    lotteryCell14=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*5+space*5, orginY+(cellHeight+space)*6, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell14];
    lotteryCell14.label.text=@"14";
    
    lotteryCell15=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*4+space*4, orginY+(cellHeight+space)*6, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell15];
    lotteryCell15.label.text=@"15";
    
    lotteryCell16=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*3+space*3,  orginY+(cellHeight+space)*6, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell16];
    lotteryCell16.label.text=@"16";
    
    lotteryCell17=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*2+space*2,  orginY+(cellHeight+space)*6, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell17];
    lotteryCell17.label.text=@"17";
    
    lotteryCell18=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*1+space*1,  orginY+(cellHeight+space)*6, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell18];
    lotteryCell18.label.text=@"18";
    
    lotteryCell19=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX+cellWidth*0+space*0,  orginY+(cellHeight+space)*6, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell19];
    lotteryCell19.label.text=@"19";
    
    
    lotteryCell20=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX, orginY+(cellHeight+space)*5, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell20];
    lotteryCell20.label.text=@"20";
    
    lotteryCell21=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX,orginY+(cellHeight+space)*4, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell21];
    lotteryCell21.label.text=@"21";
    
    lotteryCell22=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX, orginY+(cellHeight+space)*3, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell22];
    lotteryCell22.label.text=@"22";
    
    lotteryCell23=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX, orginY+(cellHeight+space)*2, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell23];
    lotteryCell23.label.text=@"23";
    
    lotteryCell24=[[YiLotteryCell alloc] initWithFrame:CGRectMake(originX, orginY+(cellHeight+space)*1, cellWidth, cellHeight)];
    [lotteryView addSubview:lotteryCell24];
    lotteryCell24.label.text=@"24";
    
    //ready
    NSArray * fontArr = [UIFont familyNames];
    for(NSString * fonName in fontArr)
    {
        NSArray * arr= [UIFont fontNamesForFamilyName:fonName];
        for(NSString * str in arr)
        {
            //   NSLog(@"fontname:%@\n",str);
        }
    }
    
    UILabel  * readyLab = [[UILabel alloc]init];
    _currentReadyLab=readyLab;
    readyLab.bounds=CGRectMake(0, 0, 110, 60);
    readyLab.center = CGPointMake(lotteryView.bounds.size.width/2.0, 230);
    readyLab.font=[UIFont fontWithName:@"GujaratiSangamMN-Bold" size:20];
    readyLab.textColor=[UIColor redColor];
    readyLab.backgroundColor=[UIColor clearColor];
    readyLab.textAlignment=NSTextAlignmentCenter;
    readyLab.text=@"Ready !";
    
    CAKeyframeAnimation * keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    NSValue * vlaue1 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
    NSValue * vlaue2 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.3, 1.3, 1.0)];
    NSValue * vlaue3 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.6, 1.6, 1.0)];
    NSValue * vlaue4 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.3, 1.3, 1.0)];
    NSValue * vlaue5 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
    keyFrameAni.values = @[vlaue1,vlaue2,vlaue3,vlaue4,vlaue5];
    keyFrameAni.duration=2.0f;
    keyFrameAni.repeatCount= HUGE_VAL;
    keyFrameAni.removedOnCompletion=YES;
    keyFrameAni.fillMode=kCAFillModeForwards;
    [_currentReadyLab.layer addAnimation:keyFrameAni forKey:nil];
    [lotteryView addSubview:readyLab];
    
    array = [[NSArray alloc] initWithObjects:lotteryCell1,lotteryCell2,lotteryCell3,lotteryCell4,lotteryCell5,lotteryCell6,lotteryCell7,lotteryCell8,lotteryCell9,lotteryCell10,lotteryCell11,lotteryCell12,lotteryCell13,lotteryCell14,lotteryCell15,lotteryCell16,lotteryCell17,lotteryCell18,lotteryCell19,lotteryCell20,lotteryCell21,lotteryCell22,lotteryCell23,lotteryCell24,nil];
    
    for (int i=0; i<array.count; i++)
    {
        YiLotteryCell *view = array[i];
        view.tag = i;
    }
    
    currentView1 = [[YiLotteryCell alloc]init];
    currentView1.backgroundColor=[UIColor redColor];
    currentView1.image=[UIImage imageNamed:@"I3"];
    currentView1.frame=lotteryCell1.frame;
    [lotteryView addSubview:currentView1];
    currentView1.hidden=YES;
    
    currentView2 = [[YiLotteryCell alloc]init];
    currentView2.backgroundColor=[UIColor greenColor];
    currentView2.image=[UIImage imageNamed:@"I3"];
    currentView2.frame=lotteryCell9.frame;
    [lotteryView addSubview:currentView2];
    currentView2.hidden=YES;
    
    currentView3=[[YiLotteryCell alloc]init];
    currentView3.backgroundColor=[UIColor blueColor];
    currentView3.image=[UIImage imageNamed:@"I3"];
    currentView3.frame=lotteryCell17.frame;
    [lotteryView  addSubview:currentView3];
    currentView3.hidden=YES;
    
    normalCurrentView=[[YiLotteryCell alloc]init];
    normalCurrentView.backgroundColor=[UIColor grayColor];
    normalCurrentView.image=[UIImage imageNamed:@"I3"];
    normalCurrentView.frame=lotteryCell1.frame;
    [lotteryView  addSubview:normalCurrentView];
    //  normalCurrentView.hidden=YES;
    
    
    UIView * infoView = [[UIView alloc]initWithFrame:CGRectMake(originX, 10, WScreen-2*originX, 40)];
    infoView.backgroundColor=[UIColor redColor];
    [lotteryView addSubview:infoView];
    
    UILabel * getLab = [[UILabel alloc]init];
    getLab.backgroundColor=[UIColor cyanColor];
    getLab.bounds=CGRectMake(0, 0, infoView.bounds.size.width/2.0, 20);
    getLab.center=CGPointMake(infoView.bounds.size.width/4.0, 10);
    getLab.text=@"获得流量";
    getLab.font=[UIFont systemFontOfSize:12];
    getLab.textAlignment=NSTextAlignmentRight;
    [infoView addSubview:getLab];
    UILabel * countLab = [[UILabel alloc]init];
    countLab.bounds=getLab.bounds;
    countLab.center=CGPointMake(getLab.center.x, getLab.center.y+getLab.bounds.size.height);
    _getExertLiuValueLab=countLab;
    countLab.textAlignment=NSTextAlignmentRight;
    countLab.font=[UIFont systemFontOfSize:17];
    countLab.text=@"00";
    [infoView addSubview:countLab];
    
    UILabel * saveLab = [[UILabel alloc]init];
    saveLab.backgroundColor=[UIColor greenColor];
    saveLab.bounds=CGRectMake(0, 0, infoView.bounds.size.width/2.0, 20);
    saveLab.center=CGPointMake(infoView.bounds.size.width*3/4.0, 10);
    saveLab.text=@"剩余流量";
    saveLab.font=[UIFont systemFontOfSize:12];
    saveLab.textAlignment=NSTextAlignmentRight;
    [infoView addSubview:saveLab];
    UILabel * countSaveLab = [[UILabel alloc]init];
    _tempCountSaveLab = countSaveLab;
    countSaveLab.bounds=getLab.bounds;
    countSaveLab.center=CGPointMake(saveLab.center.x, saveLab.center.y+saveLab.bounds.size.height);
    countSaveLab.textAlignment=NSTextAlignmentRight;
    countSaveLab.text=@"1000";
    _totalFlowCount=1000;
    countLab.font=[UIFont systemFontOfSize:17];
    [infoView addSubview:countSaveLab];
    
    
    UIView * qesView = [[UIView alloc]initWithFrame:CGRectMake(originX, 320, WScreen-2*originX, 40)];
    qesView.backgroundColor=[UIColor greenColor];
    [lotteryView addSubview:qesView];
    
    UIButton * queBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    queBtn.frame=CGRectMake(0, 0, 40, 40);
    queBtn.backgroundColor=[UIColor redColor];
    [queBtn setTitle:@"玩法" forState:UIControlStateNormal];
    [queBtn addTarget:self action:@selector(questionClick:) forControlEvents:UIControlEventTouchUpInside];
    [qesView addSubview:queBtn];
    
    UIButton * initDataBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    initDataBtn.frame=CGRectMake(50, 0, 60, 40);
    initDataBtn.backgroundColor=[UIColor redColor];
    [initDataBtn setTitle:@"重押" forState:UIControlStateNormal];
    [initDataBtn addTarget:self action:@selector(initDataBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [qesView addSubview:initDataBtn];
    
    UIButton*  startButton1=[UIButton buttonWithType:UIButtonTypeCustom];
    startButton1.frame=CGRectMake(qesView.bounds.size.width-80 , 0  , 80, 40);
    startButton1.backgroundColor=[UIColor blackColor];
    [startButton1 setTitle:@"开始游戏" forState:UIControlStateNormal];
    startButton1.titleLabel.font=[UIFont systemFontOfSize:12];
    [startButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [qesView addSubview:startButton1];
    [startButton1 addTarget:self action:@selector(prepareLotteryAction) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat x1 = 10.0;
    CGFloat seperateX = 3.0;
    CGFloat  btnWid = (WScreen-7*seperateX - 2*x1)/8.0;
    for(int i =0;i<8;i++)
    {
        CGFloat x2 = x1 + i*seperateX+i*btnWid;
        YzDownView * vi= [[YzDownView alloc]initWithFrame:CGRectMake(x2, 400, btnWid, 70)];
        [vi.changeBtn addTarget:self action:@selector(valueBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        vi.changeBtn.tag=i+1;
        [lotteryView addSubview:vi];
        [_downBtnArr addObject:vi];
    }
}
-(void)valueBtnClick:(UIButton*)sender
{
    YzDownView * vi = _downBtnArr[sender.tag-1];
    
    if(_totalFlowCount<=0)
        return;
    
    int ab= vi.valueLab.text.intValue;
    ab++;
    
    _totalFlowCount--;
    _tempCountSaveLab.text=[NSString stringWithFormat:@"%d",_totalFlowCount];
    
    NSString * countStr=@"";
    if(ab<10&&ab>=0)
    {
        countStr = [NSString stringWithFormat:@"0%d",ab];
        vi.valueLab.text=countStr;
    }
    else if (ab>=10&&ab<=99)
    {
        countStr = [NSString stringWithFormat:@"%d",ab];
        vi.valueLab.text=countStr;
    }
    else
    {
        
    }
    
}


-(void)questionClick:(id)sender
{
    NSLog(@"游戏 玩法");
}
-(void)initDataBtnClick:(UIButton *)sender
{
    NSLog(@"重押，数据初始化");
    int ab=0;
    
    for(YzDownView * vi  in _downBtnArr)
    {
        ab+=vi.valueLab.text.intValue;
        vi.valueLab.text=@"00";
    }
    
    _totalFlowCount+=ab;
    _tempCountSaveLab.text=[NSString stringWithFormat:@"%d",_totalFlowCount];
}


- (void)prepareLotteryAction
{
    
    //  [self doAniNormalView:NO withStopStation:10];
    
    BOOL isPlay=NO;
    for(YzDownView * vi in _downBtnArr)
    {
        int ab  = vi.valueLab.text.intValue;
        if(ab>0)
        {
            isPlay = YES;
            break;
        }
    }
    
    if(!isPlay)
        return;
    
    
    _aKey = arc4random()%24+1;
    _bKey= arc4random()%24+1;
    _cKey=arc4random()%24+1;
    
    
    
    [_currentReadyLab.layer removeAllAnimations];
    _currentReadyLab.text=@"GO !";
    CAKeyframeAnimation * keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    NSValue * vlaue1 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
    NSValue * vlaue2 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.3, 1.3, 1.0)];
    NSValue * vlaue3 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.6, 1.6, 1.0)];
    NSValue * vlaue4 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.3, 1.3, 1.0)];
    NSValue * vlaue5 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
    keyFrameAni.values = @[vlaue1,vlaue2,vlaue3,vlaue4,vlaue5];
    keyFrameAni.duration=2.0f;
    keyFrameAni.repeatCount= 3;
    keyFrameAni.removedOnCompletion=YES;
    keyFrameAni.delegate=self;
    keyFrameAni.fillMode=kCAFillModeForwards;
    [_currentReadyLab.layer addAnimation:keyFrameAni forKey:@"goAnimation"];
    
    NSArray * resultValueArr =@[@(_aKey),@(_bKey),@(_cKey)];
    NSMutableDictionary *yzValueDic=[NSMutableDictionary dictionaryWithCapacity:0];
    
    for(YzDownView * vi in _downBtnArr)
    {
        int ab  = vi.valueLab.text.intValue;
        int viTag= vi.changeBtn.tag;
        NSLog(@"vitab----%d---%d",viTag,ab);
        if(ab>0)
        {
            [yzValueDic setObject:@(ab) forKey:@(viTag)];
        }
    }
    
    
    
    
    int x = 0;
    if(yzValueDic.allKeys.count>0)
    {
        if(resultValueArr.count>0)
        {
            for(NSNumber * num in resultValueArr)
            {
                int a = [num intValue];
                int b = a%3;
                if(b>0)
                {
                    b=a/3+1;
                }
                else
                {
                    b=a/3;
                }
                NSArray *arr = yzValueDic.allKeys;
                if([arr containsObject:@(b)])
                {
                    int va = [[yzValueDic objectForKey:@(b)]intValue];
                    x= x + 1*va;
                }
            }
            
        }
        
    }
    
    NSArray *taoArr= yzValueDic.allValues;
    int y = 0 ;
    
    
    for(NSNumber *num in taoArr)
    {
        y+=[num intValue];
    }
    int yy = -y+x;
    
    _getFlowCount+=yy;
    _getExertLiuValueLab.text=[NSString stringWithFormat:@"%d",_getFlowCount];
    _totalFlowCount+=x;
    _tempCountSaveLab.text=[NSString stringWithFormat:@"%d",_totalFlowCount];
}



-(void)doAniNormalView:(BOOL)isLucky withStopStation:(int)aStation
{
    int  a1 = aStation;
    normalCurrentView.hidden=NO;
    CAKeyframeAnimation * animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSMutableArray * arr1 = [NSMutableArray arrayWithCapacity:0];
    for(int i=0;i<96;i++)
    {
        int j=i%24+1;
        UIView * vi = array[j-1];
        CGPoint point = vi.center;
        NSValue *value=[NSValue valueWithCGPoint:point];
        [arr1 addObject:value];
    }
    
    int resuLenth = a1-0;
    for(int k=0;k<resuLenth;k++)
    {
        UIView * vi1 = array[k];
        CGPoint point1 = vi1.center;
        NSValue *value1=[NSValue valueWithCGPoint:point1];
        [arr1 addObject:value1];
    }
    animation1.duration=12+arr1.count*1/12.0;
    animation1.values=arr1;
    animation1.speed=3.50;
    animation1.delegate=self;
    if(isLucky)
    {
        animation1.repeatCount=3;
    }
    animation1.removedOnCompletion=NO;
    animation1.fillMode= kCAFillModeForwards;
    animation1.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [normalCurrentView.layer addAnimation:animation1 forKey:@"animationNormal"];
}

-(void)doAnimationSanViewWithFirstStation:(int)aSta1 secStation:(int)aSta2 thirdStation:(int)aSta3
{
    int  a1 = aSta1;
    int  a2= aSta2;
    int  a3 =aSta3;
    
    currentView1.hidden=NO;
    CAKeyframeAnimation * animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSMutableArray * arr1 = [NSMutableArray arrayWithCapacity:0];
    for(int i=0;i<96+72;i++)
    {
        int j=i%24+1;
        UIView * vi = array[j-1];
        CGPoint point = vi.center;
        NSValue *value=[NSValue valueWithCGPoint:point];
        [arr1 addObject:value];
    }
    
    int resuLenth = a1-0;
    for(int k=0;k<resuLenth;k++)
    {
        UIView * vi1 = array[k];
        CGPoint point1 = vi1.center;
        NSValue *value1=[NSValue valueWithCGPoint:point1];
        [arr1 addObject:value1];
    }
    animation1.duration=20+arr1.count*1/12.0;
    animation1.values=arr1;
    animation1.speed=3.6;
    animation1.delegate=self;
    animation1.removedOnCompletion=NO;
    animation1.fillMode= kCAFillModeForwards;
    animation1.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [currentView1.layer addAnimation:animation1 forKey:@"animation1"];
    
    currentView2.hidden=NO;
    CAKeyframeAnimation * animation2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSMutableArray * arr2 = [NSMutableArray arrayWithCapacity:0];
    for(int i=8;i<104+72;i++)
    {
        int j=i%24+1;
        UIView * vi = array[j-1];
        CGPoint point = vi.center;
        NSValue *value=[NSValue valueWithCGPoint:point];
        [arr2 addObject:value];
    }
    if(a2<=8)
    {
        for(int i=8;i<24;i++)
        {
            int j=i%24+1;
            UIView * vi = array[j-1];
            CGPoint point = vi.center;
            NSValue *value=[NSValue valueWithCGPoint:point];
            [arr2 addObject:value];
        }
        
        for(int i=0;i<a2;i++)
        {
            int j=i%24+1;
            UIView * vi = array[j-1];
            CGPoint point = vi.center;
            NSValue *value=[NSValue valueWithCGPoint:point];
            [arr2 addObject:value];
        }
    }
    else
    {
        for(int i=8;i<a2;i++)
        {
            int j=i%24+1;
            UIView * vi = array[j-1];
            CGPoint point = vi.center;
            NSValue *value=[NSValue valueWithCGPoint:point];
            [arr2 addObject:value];
        }
    }
    
    animation2.duration=20+arr2.count*1/12.0;
    animation2.values=arr2;
    animation2.speed=3.4;
    animation2.delegate=self;
    animation2.removedOnCompletion=NO;
    animation2.fillMode= kCAFillModeForwards;
    animation2.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [currentView2.layer addAnimation:animation2 forKey:@"animation2"];
    
    
    currentView3.hidden=NO;
    CAKeyframeAnimation * animation3 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSMutableArray * arr3 = [NSMutableArray arrayWithCapacity:0];
    for(int i=16;i<112+72;i++)
    {
        int j=i%24+1;
        UIView * vi = array[j-1];
        CGPoint point = vi.center;
        NSValue *value=[NSValue valueWithCGPoint:point];
        [arr3 addObject:value];
    }
    if(a3<=16)
    {
        for(int i=16;i<24;i++)
        {
            int j=i%24+1;
            UIView * vi = array[j-1];
            CGPoint point = vi.center;
            NSValue *value=[NSValue valueWithCGPoint:point];
            [arr3 addObject:value];
        }
        
        for(int i=0;i<a3;i++)
        {
            int j=i%24+1;
            UIView * vi = array[j-1];
            CGPoint point = vi.center;
            NSValue *value=[NSValue valueWithCGPoint:point];
            [arr3 addObject:value];
        }
    }
    else
    {
        for(int i=16;i<a3;i++)
        {
            int j=i%24+1;
            UIView * vi = array[j-1];
            CGPoint point = vi.center;
            NSValue *value=[NSValue valueWithCGPoint:point];
            [arr3 addObject:value];
        }
    }
    
    animation3.duration=20+arr3.count*1/12.0;
    animation3.values=arr3;
    animation3.delegate=self;
    animation3.speed=3.50;
    animation3.removedOnCompletion=NO;
    animation3.fillMode= kCAFillModeForwards;
    animation3.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [currentView3.layer addAnimation:animation3 forKey:@"animation3"];
    
    NSLog(@"%@---%@---%@",animation1,animation2,animation3);
    
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    static  int thirdCount=0;
    
    if(anim == [normalCurrentView.layer animationForKey:@"animationNormal"])
    {
        NSLog(@"1动画正常完成 了----%@",anim);
    }
    
    if (anim==[currentView1.layer animationForKey:@"animation1"])
    {
        NSLog(@"currentView1动画正常完成 了----%@",anim);
        thirdCount++;
    }
    
    if (anim==[currentView2.layer animationForKey:@"animation2"])
    {
        NSLog(@"currentView2动画正常完成 了----%@",anim);
        thirdCount++;
    }
    if (anim==[currentView3.layer animationForKey:@"animation3"])
    {
        NSLog(@"currentView3动画正常完成 了----%@",anim);
        thirdCount++;
    }
    
    if([_currentReadyLab.text isEqualToString:@"GO !"])
    {
        NSLog(@"倒计时完成");
        _currentReadyLab.text=@"";
        
        [self doAnimationSanViewWithFirstStation:_aKey secStation:_bKey thirdStation:_cKey];
    }
    
    if(thirdCount>=3)
    {
        NSLog(@"lucky,3个一块完成了");
        thirdCount=0;
        
        
        NSString * str = [NSString stringWithFormat:@"本次中奖号码：%d,%d,%d",_aKey,_bKey,_cKey];
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"中奖号码" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        
        for(YzDownView * vi  in _downBtnArr)
        {
            vi.valueLab.text=@"00";
        }
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
